package com.ruicar.afs.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.admin.entity.SysDepartment;
import com.ruicar.afs.cloud.admin.mapper.SysDepartmentMapper;
import com.ruicar.afs.cloud.admin.service.SysDepartmentService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * The type SysDepartmentServiceImpl.
 * <p>
 * Created on 2019-02-21
 *
 * @author Fzero
 */
@Service
public class SysDepartmentServiceImpl extends ServiceImpl<SysDepartmentMapper, SysDepartment> implements SysDepartmentService {
	/**
	 * 通过父id获取 升序
	 *
	 * @param parentId
	 * @return
	 */
	@Override
	public List<SysDepartment> findByParentIdOrderBySortOrder(String parentId) {
		return baseMapper.selectList(Wrappers.<SysDepartment>query().lambda().eq(SysDepartment::getParentId, parentId).orderByAsc(SysDepartment::getSortOrder));
	}

	/**
	 * 通过父id和状态获取
	 *
	 * @param parentId
	 * @param status
	 * @return
	 */
	@Override
	public List<SysDepartment> findByParentIdAndStatusOrderBySortOrder(String parentId, String status) {
		return baseMapper.selectList(Wrappers.<SysDepartment>query().lambda().eq(SysDepartment::getParentId, parentId).eq(SysDepartment::getStatus, status));
	}

	/**
	 * 部门名模糊搜索 升序
	 *
	 * @param title
	 * @return
	 */
	@Override
	public List<SysDepartment> findByTitleLikeOrderBySortOrder(String title) {
		return baseMapper.selectList(Wrappers.<SysDepartment>query().lambda().like(SysDepartment::getTitle, title).orderByAsc(SysDepartment::getSortOrder));
	}


	@Override
	public List<SysDepartment> findAllSubByParentId(Long parentId) {
		List<SysDepartment> queryList = this.baseMapper.selectList(Wrappers.<SysDepartment>lambdaQuery().eq(SysDepartment::getParentId,parentId));
		if(queryList.size()==0){
			return queryList;
		}
		List<SysDepartment> rtnList = new ArrayList<>();
		queryList.forEach(dept->{
			rtnList.add(dept);
			rtnList.addAll(findAllSubByParentId(dept.getId()));
		});
		return rtnList;
	}

}
